<?php

/**
 *TrainerCity 商品管理程序 *
 * $Author: wangleisvn $
 * $Id: goods.php 17114 2010-04-16 07:13:03Z wangleisvn $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
include_once(ROOT_PATH . '/includes/lib_main.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table('goods'), $db, 'goods_id', 'goods_name');

/*------------------------------------------------------ */
//-- Proposals List
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	/*查找proposals表，获得所有任务信息*/
	$sql = "SELECT p.*,u.user_name "." FROM " . $GLOBALS['ecs']->table('proposals').' AS p JOIN '.$GLOBALS['ecs']->table('users') . ' AS u '." WHERE p.uid=u.user_id ";
	$proposals= $db->getAll($sql);	
	
	$taskcount=count($proposals);		
	$record_count = $taskcount;
	$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
	$pagesize = 10;
	$pager = get_pager('proposals.php', array('act' => 'list'), $record_count, $page, $pagesize);
	$fitness_lists = get_proposal_list($pagesize, $pager['start']);
	
	$smarty->assign('proposals_list',   $fitness_lists);
  	$smarty->assign('pager', $pager);
    $smarty->assign('full_page',    1);
	$smarty->assign('action_link', array('text' => 'Add New Type', 'href' => 'proposals.php?act=add_proposal'));
	$smarty->display('proposals_list.htm');
}

/*------------------------------------------------------ */
//-- Proposals List
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'proposals_list')
{
	/*获取方案id*/
	$getpid  = isset($_GET['pid']) ? intval($_GET['pid']) : 'nothing';
	
	/*根据传进来的pid值，查找proposals表，获得任务信息*/
	$sql = "SELECT p.*,u.user_name "." FROM " . $GLOBALS['ecs']->table('proposals').' AS p JOIN '.$GLOBALS['ecs']->table('users') . ' AS u '." WHERE p.pid='$getpid' AND p.uid=u.user_id ";
	$proposals= $db->getAll($sql);	
	$smarty->assign('proposals_list',   $proposals);
  
    $smarty->assign('full_page',    1);
	$smarty->assign('action_link', array('text' => 'Add New Type', 'href' => 'proposals.php?act=add_proposal'));
	$smarty->display('proposals_list.htm');
}

/*------------------------------------------------------ */
//-- Proposal Detail
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'proposal_detail')
{
	
	/*获取方案id*/
	$getpid  = isset($_GET['pid']) ? intval($_GET['pid']) : 'nothing';
	$getppid  = isset($_GET['ppid']) ? intval($_GET['ppid']) : 'nothing';
	
	#根据传进来的ppid值，查找proposals表，获得方案信息
	$sql = "SELECT * "." FROM " . $GLOBALS['ecs']->table('proposals') . " WHERE ppid='$getppid' ";
	$proposal = $db->getRow($sql);
	
	#获取任务详细信息
	$sql = 'SELECT a.*,b.budgetType,u.user_name,g.genderType ' .'FROM ' . $GLOBALS['ecs']->table('projects') . ' AS a JOIN '. $GLOBALS['ecs']->table('budget') . ' AS b JOIN ' .$GLOBALS['ecs']->table('users') . ' AS u JOIN ' .$GLOBALS['ecs']->table('gender')." AS g "." WHERE a.pid='$getpid' AND a.budget=b.num AND a.uid=u.user_id AND u.sex=g.xid ";
	$project=$db->getRow($sql);
	
	/*获取可选择信息*/
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainertype')." WHERE num='$project[trainerType]' ";
	$trainertype=$db->getRow($sql);

	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainingtype')." WHERE num='$project[trainingType]' ";
	$trainingtype=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionstype')." WHERE num='$project[sessions]' ";
	$sessionstype=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionlength')." WHERE num='$project[length]' ";
	$sessionlength=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('budget')." WHERE num='$project[budget]' ";
	$budget=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('quoting')." WHERE num='$project[quoting]' ";
	$quoting=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('friendsnum')." WHERE num='$project[friends]' ";
	$friendsnum=$db->getRow($sql);
			
	$smarty->assign('trainer_type', $trainertype);
	$smarty->assign('training_type', $trainingtype);
	$smarty->assign('sessions_type', $sessionstype);
	$smarty->assign('session_length', $sessionlength);
	$smarty->assign('budget', $budget);
	$smarty->assign('quoting', $quoting);
	$smarty->assign('friends_num', $friendsnum);
	$smarty->assign('project', $project);
	$smarty->assign('proposal', $proposal);
	$smarty->display('proposal_info.htm');   
}

/*------------------------------------------------------ */
//-- Edit Proposal
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'act_EditClientProposal')
{
	/*获取方案id*/
	$getppid  = isset($_REQUEST['ppid']) ? trim($_REQUEST['ppid']) : 'nothing';
	$addproposal['pid'] = isset($_REQUEST['pid']) ? trim($_REQUEST['pid']) : 'nothing';
	$addproposal['uid'] = isset($_REQUEST['uid']) ? trim($_REQUEST['uid']) : 'nothing';
	$proposal_subject = !empty($_POST['proposal_subject']) ? trim($_POST['proposal_subject']) : '';
	
	#保存用户提交的附件
		if($_FILES['userfile']['error']>0)
		{
			echo 'Problem:';
			switch($_FILES['userfile']['error'])
			{
				case 1:	echo 'File exceeded upload_max_filesize';break;
				case 2:	echo 'File exceeded max_file_size';break;
				case 3: echo 'File only partially uploaded';break;
				case 4: echo 'No file uploaded';break;
			}
			exit;
		}

		if($_FILES['userfile']['type'] != 'text/plain')
		{
			echo 'Problem: file is not plain text';
			exit;
		}

		$filename = $_FILES['userfile']['name']."_$addproposal[pid]_"."$addproposal[uid]";
		$upfile = '..\uploads\\'.$filename;

		if(is_uploaded_file($_FILES['userfile']['tmp_name']))
		{
			if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile))
			{
				echo 'Problem: Could not move file to destination directory';
				exit;
			}
		}
		else
		{
			echo 'Problem: Possible file upload attack. Filename:';
			echo $_FILES['userfile']['name'];
			exit;
		}
	
		$addproposal['proposaledDate']= date("Y-m-d H:i:s",time());
		$addproposal['accessories']=$filename;
		
		#更新数据库
		$sql = "UPDATE " . $ecs->table('proposals') . " SET proposalSubject='$proposal_subject',accessories='$addproposal[accessories]',proposaledDate='$addproposal[proposaledDate]' "."WHERE ppid='".$getppid."' ";
		if($db->query($sql) != false)
		{
			/*Add Successfully*/	
			$links = array(array('href' => 'proposals.php?act=list', 'text' => "Proposals List"));
        	sys_msg("Update Proposal Successfully!", 0, $links);
		}
}

/*------------------------------------------------------ */
//-- Add Proposal
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add_proposal')
{
	/*获取方案id*/
	$getpid  = isset($_GET['pid']) ? intval($_GET['pid']) : 'nothing';
	
	#获取任务详细信息
	$sql = 'SELECT a.*,b.budgetType,u.user_name,g.genderType ' .'FROM ' . $GLOBALS['ecs']->table('projects') . ' AS a JOIN '. $GLOBALS['ecs']->table('budget') . ' AS b JOIN ' .$GLOBALS['ecs']->table('users') . ' AS u JOIN ' .$GLOBALS['ecs']->table('gender')." AS g "." WHERE a.pid='$getpid' AND a.budget=b.num AND a.uid=u.user_id AND u.sex=g.xid ";
	$project=$db->getRow($sql);
	
	/*获取可选择信息*/
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainertype')." WHERE num='$project[trainerType]' ";
	$trainertype=$db->getRow($sql);

	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainingtype')." WHERE num='$project[trainingType]' ";
	$trainingtype=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionstype')." WHERE num='$project[sessions]' ";
	$sessionstype=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionlength')." WHERE num='$project[length]' ";
	$sessionlength=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('budget')." WHERE num='$project[budget]' ";
	$budget=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('quoting')." WHERE num='$project[quoting]' ";
	$quoting=$db->getRow($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('friendsnum')." WHERE num='$project[friends]' ";
	$friendsnum=$db->getRow($sql);
	
	$sql = "SELECT user_id,user_name FROM " .$GLOBALS['ecs']->table('users')." WHERE usertype=3 ";
	$users=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('proposaledtrainingtype');
	$proposaledtrainingtype=$db->getAll($sql);
			
	$smarty->assign('trainer_type', $trainertype);
	$smarty->assign('training_type', $trainingtype);
	$smarty->assign('sessions_type', $sessionstype);
	$smarty->assign('session_length', $sessionlength);
	$smarty->assign('budget', $budget);
	$smarty->assign('quoting', $quoting);
	$smarty->assign('friends_num', $friendsnum);
	$smarty->assign('users', $users);
	$smarty->assign('proposaledtrainingtype', $proposaledtrainingtype);
	$smarty->assign('project', $project);	
	$smarty->display('proposal_add.htm');  
}

/*------------------------------------------------------ */
//-- Add New Proposal
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'act_add_proposal')
{
	
	$proposaled_training_type = $_POST['proposaled_training_type'];	
	$approx_budget = $_POST['approx_budget'];
	$proposal_subject = $_POST['proposal_subject'];
	$addproposal['pid'] = $_GET['pid'];
	$addproposal['uid'] = $_POST['user_id'];
	$addproposal['tid'] = 0;
	$addproposal['priceFlag']=0;
		
	#保存用户提交的附件
	if($_FILES['userfile']['error']>0)
	{
		echo 'Problem:';
		switch($_FILES['userfile']['error'])
		{
			case 1:	echo 'File exceeded upload_max_filesize';break;
			case 2:	echo 'File exceeded max_file_size';break;
			case 3: echo 'File only partially uploaded';break;
			case 4: echo 'No file uploaded';break;
		}
		exit;
	}

	if($_FILES['userfile']['type'] != 'text/plain')
	{
		echo 'Problem: file is not plain text';
		exit;
	}

	$filename = $_FILES['userfile']['name']."_$addproposal[pid]_"."$addproposal[uid]";
	$upfile = '..\uploads\\'.$filename;

	if(is_uploaded_file($_FILES['userfile']['tmp_name']))
	{
		if(!move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile))
		{
			echo 'Problem: Could not move file to destination directory';
			exit;
		}
	}
	else
	{
		echo 'Problem: Possible file upload attack. Filename:';
		echo $_FILES['userfile']['name'];
		exit;
	}
	
	$addproposal['proposaledDate']= date("Y-m-d H:i:s",time());
	$addproposal['accessories']=$filename;
		
	/*插入数据库*/
	$sql = "INSERT INTO " . $ecs->table('proposals') . "(pid,uid,tid,proposaledTrainingType,priceFlag,approxPrice,proposalSubject,proposaledDate,accessories) VALUES ('" . $addproposal['pid'] . "', '" . $addproposal['uid'] . "', '" . $addproposal['tid'] . "', '" . $proposaled_training_type . "','" . $addproposal['priceFlag'] . "', '" . $approx_budget . "', '" . $proposal_subject . "','" . $addproposal['proposaledDate'] . "', '" . $addproposal['accessories'] . "')";

    if($db->query($sql) != false)
	{
		/*方案投标成功*/	
	}
	else
	{
		/*Add Failed*/	
		$links = array(array('href' => 'proposals.php?act=list', 'text' => "Proposals List"));
        sys_msg("Insert Proposal Into The Database Failed, please try again!", 0, $links);
	} 
		
	#trainer表中projectsEnter加1
	$sql = "SELECT projectsEntered "." FROM " . $GLOBALS['ecs']->table('trainer')."WHERE user_id='".$addproposal['uid']."' ";
	$value = $db->getRow($sql);
	$value['projectsEntered'] = $value['projectsEntered'] + 1;
		
	$sql = "UPDATE " . $ecs->table('trainer') . " SET projectsEntered='".$value['projectsEntered']."' "." WHERE user_id='".$addproposal['uid']."' ";
	$db->query($sql);
		
	#projects表中proposalsNum加1
	$sql = "SELECT proposalsNum "." FROM " . $GLOBALS['ecs']->table('projects')."WHERE pid='".$addproposal[pid]."' ";
	$value = $db->getRow($sql);
	$value['proposalsNum'] = $value['proposalsNum'] + 1;	
	$sql = "UPDATE " . $ecs->table('projects') . " SET proposalsNum='".$value[proposalsNum]."' "."WHERE pid='".$addproposal[pid]."' ";
			
	if($db->query($sql) != false)
	{
		/*Add Successfully*/	
		$links = array(array('href' => 'proposals.php?act=list', 'text' => "Proposals List"));
        sys_msg("New Proposal Add Successfully!", 0, $links);
	}
	else
	{
		/*Add Failed*/	
		$links = array(array('href' => 'proposals.php?act=list', 'text' => "Proposals List"));
        sys_msg("New Proposal Add Failed, please try again!", 0, $links);
	} 
}

/*------------------------------------------------------ */
//-- Proposal Delete
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'proposal_delete')
{
	/*获取方案id*/
	$getppid  = isset($_REQUEST['ppid']) ? trim($_REQUEST['ppid']) : 'nothing';
	
	#add by wanglei
	$sql = "DELETE FROM " . $GLOBALS['ecs']->table('proposals') .
            " WHERE ppid='$getppid' ";
	if($db->query($sql) != false)
	{
		/*Delete Successfully*/	
		$links = array(array('href' => 'proposals.php?act=list', 'text' => "Proposals List"));
        sys_msg("Delete Type Successfully!", 0, $links);
			
	}
	else
	{
		/*Delete Failed*/	
		$links = array(array('href' => 'proposals.php?act=list', 'text' => "Proposals List"));
        sys_msg("Failed, please try again!", 0, $links);
	}	
}

/**
 * 
 *
 * @param   integer $num
 * @param   integer $start
 *
 * @return  array
 */
function get_proposal_list($num, $start, $temp='', $sort='ppid')
{
    /*   */	
	$sql = "SELECT pp.*,u.user_name, p.subject "." FROM " . $GLOBALS['ecs']->table('proposals').' AS pp JOIN '.$GLOBALS['ecs']->table('users') . ' AS u JOIN '. $GLOBALS['ecs']->table('projects'). ' AS p '." WHERE pp.uid=u.user_id AND pp.pid=p.pid ".$temp;
	$sql = $sql." ORDER by ". $sort." DESC LIMIT ".$start.",".$num;
	$res = $GLOBALS['db']->getAll($sql);
	
    return $res;
}

?>